package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.cache.GmallCache;
import com.atguigu.gmall.product.mapper.SpuSaleAttrMapper;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-04-18 14:19
 */
@Service
public class SpuManageServiceImpl implements SpuManageService {

    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuPosterService spuPosterService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Autowired
    private BaseSaleAttrService baseSaleAttrService;


    /**
     * 根据分类查询商品SPU列表
     *
     * @param iPage
     * @param category3Id
     * @return
     */
    @Override
    public Page<SpuInfo> getSpuListByPage(Page<SpuInfo> iPage, Long category3Id) {
        //1.构建查询查询 排序
        LambdaQueryWrapper<SpuInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuInfo::getCategory3Id, category3Id);
        queryWrapper.orderByDesc(SpuInfo::getUpdateTime);
        //2.执行分页查询
        iPage = spuInfoService.page(iPage, queryWrapper);
        return iPage;
    }

    @Override
    public List<BaseSaleAttr> getBaseSaleAttrList() {
        return baseSaleAttrService.list();
    }


    /**
     * 保存商品SPU信息
     * 1.将商品基本信息封装SpuInfo对象 存入spu_info表
     * 2.将提交商品图片封装到SpuImage集合中 存入spu_image表
     * 3.将提交海报图片封装到SpuPost集合中 存入spu_poster表
     * 4.将提交销售属性名称SpuSaleAttr集合 存入spu_sale_attr表
     * 5.将提交销售属性值SpuSaleAttrValue集合 存入spu_sale_attr_value表
     *
     * @param spuInfo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSpuInfo(SpuInfo spuInfo) {
        //1.将商品基本信息封装SpuInfo对象 存入spu_info表
        spuInfoService.save(spuInfo);
        Long spuId = spuInfo.getId();
        //2.将提交商品图片封装到SpuImage集合中 存入spu_image表
        List<SpuImage> spuImageList = spuInfo.getSpuImageList();
        if (!CollectionUtils.isEmpty(spuImageList)) {
            spuImageList.stream().forEach(spuImage -> {
                spuImage.setSpuId(spuId);
            });
            //批量执行保存
            spuImageService.saveBatch(spuImageList);
        }
        //3.将提交海报图片封装到SpuPost集合中 存入spu_poster表
        List<SpuPoster> spuPosterList = spuInfo.getSpuPosterList();
        if (!CollectionUtils.isEmpty(spuPosterList)) {
            spuPosterList.stream().forEach(spuPoster -> {
                spuPoster.setSpuId(spuId);
            });
            spuPosterService.saveBatch(spuPosterList);
        }
        //4.将提交销售属性名称SpuSaleAttr集合 存入spu_sale_attr表
        List<SpuSaleAttr> spuSaleAttrList = spuInfo.getSpuSaleAttrList();
        if (!CollectionUtils.isEmpty(spuSaleAttrList)) {
            spuSaleAttrList.stream().forEach(spuSaleAttr -> {
                spuSaleAttr.setSpuId(spuId);
                //5.将提交销售属性值SpuSaleAttrValue集合 存入spu_sale_attr_value表
                List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
                if (!CollectionUtils.isEmpty(spuSaleAttrValueList)) {
                    spuSaleAttrValueList.stream().forEach(spuSaleAttrValue -> {
                        //关联商品SPUID
                        spuSaleAttrValue.setSpuId(spuId);
                        //为平台属性名称赋值
                        spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                    });
                    spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
                }
            });
            spuSaleAttrService.saveBatch(spuSaleAttrList);
        }
    }

    /**
     * 根据商品SPUID查询商品SPU所有销售属性-包含销售属性值
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuSaleAttr> getSpuSaleAttrList(Long spuId) {
        //1.获取销售属性Mapper对象 调用自定义SQL
        SpuSaleAttrMapper spuSaleAttrMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        List<SpuSaleAttr> list = spuSaleAttrMapper.getSpuSaleAttrList(spuId);
        //2.返回结果
        return list;
    }


    /**
     * 根据商品SPUID查询商品所有图片
     *
     * @param spuId
     * @return
     */
    @Override
    public List<SpuImage> getSpuImageList(Long spuId) {
        LambdaQueryWrapper<SpuImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuImage::getSpuId, spuId);
        return spuImageService.list(queryWrapper);
    }

    /**
     * 根据SPUID查询商品所有海报列表
     *
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "spuPoster:")
    public List<SpuPoster> getSpuPosterBySpuId(Long spuId) {
        LambdaQueryWrapper<SpuPoster> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpuPoster::getSpuId, spuId);
        return spuPosterService.list(queryWrapper);
    }

    /**
     * 查询SPU所有销售属性,将指定传入SKU商品销售属性区分是否选中
     *
     * @param skuId
     * @param spuId
     * @return
     */
    @Override
    @GmallCache(prefix = "spuSaleAttrList:")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(Long skuId, Long spuId) {
        //获取销售属性持久层Mapper对象
        SpuSaleAttrMapper spuSaleAttrBaseMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        //调用自定义SQL查询
        List<SpuSaleAttr> list = spuSaleAttrBaseMapper.getSpuSaleAttrListCheckBySku(skuId, spuId);
        return list;
    }


    /**
     * 查询每个商品SKU 销售属性 跟 SKUID 对照关系
     *
     * @param spuId
     * @return {"3732|3734":24,"3732|3735":25}
     */
    @Override
    @GmallCache(prefix = "skuValueIdsMap:")
    public Map<String, String> getSkuValueIdsMap(Long spuId) {
        HashMap<String, String> mapResult = new HashMap<>();
        //获取销售属性持久层Mapper对象
        SpuSaleAttrMapper spuSaleAttrBaseMapper = (SpuSaleAttrMapper) spuSaleAttrService.getBaseMapper();
        List<Map<String, Object>> mapList = spuSaleAttrBaseMapper.getSkuValueIdsMap(spuId);
        if (!CollectionUtils.isEmpty(mapList)) {
            mapList.stream().forEach(skuSaleAttrValuMap -> {
                String skuId = skuSaleAttrValuMap.get("sku_id").toString();
                String valueIds = skuSaleAttrValuMap.get("value_ids").toString();
                mapResult.put(valueIds, skuId);
            });
        }
        return mapResult;
    }
}
